package com.cnb.telepathytalk.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cnb.telepathytalk.R;
import com.cnb.telepathytalk.dto.PersonDTO;
import com.cnb.telepathytalk.util.LazyImageLoader;

public class NoneFriendListAdapter extends BaseAdapter {
	private ArrayList<PersonDTO> list;
	private int layoutResId;
	private LayoutInflater inflater;
	private LazyImageLoader imageLoader;

	public NoneFriendListAdapter(Context context) {
		super();
		this.list = new ArrayList<PersonDTO>();
		this.layoutResId = R.layout.list_item_none_friend;
		this.inflater = LayoutInflater.from(context);
		this.imageLoader = new LazyImageLoader(context, R.drawable.profile_freind, 50, LazyImageLoader.SET_IMAGE);
	}

	public ArrayList<PersonDTO> getList() {
		return list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public PersonDTO getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(layoutResId, null);
			holder = new Holder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}

		PersonDTO item = getItem(position);

		imageLoader.DisplayImage(item.getProfileUrls(0), holder.getProfile());

		holder.getName().setText(item.getName());
		holder.getStatusMsg().setText("" + item.getAge());

		if (item.getGender() == PersonDTO.GENDER_MAN) {
			holder.getStatusMsg().setCompoundDrawablesWithIntrinsicBounds(R.drawable.man, 0, 0, 0);
		} else if (item.getGender() == PersonDTO.GENDER_WOMAN) {
			holder.getStatusMsg().setCompoundDrawablesWithIntrinsicBounds(R.drawable.woman, 0, 0, 0);
		}

		return convertView;
	}

	private class Holder {
		private View baseView;
		private ImageView profile;
		private TextView name;
		private TextView statusMsg;

		public Holder(View view) {
			super();
			baseView = view;
		}

		public ImageView getProfile() {
			if (profile == null)
				profile = (ImageView) baseView.findViewById(R.id.profileImage);
			return profile;
		}

		public TextView getName() {
			if (name == null)
				name = (TextView) baseView.findViewById(R.id.name);
			return name;
		}

		public TextView getStatusMsg() {
			if (statusMsg == null)
				statusMsg = (TextView) baseView.findViewById(R.id.statusMsg);
			return statusMsg;
		}
	}
}